// pages/houseSeller/houseSeller.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    isHalf: true,
    houseFlag: '',
    releaseUserId: '',
    houseType: '',
    publisher:'',
    currentPage: 1,
    houseList:[],
    userInfo:{},
    wholeMine:false,
    loadSuccess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading()
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    if (options.item){
      this.setData({
        publisher: JSON.parse(options.item)
      })
    }
    this.setData({
      houseType: options.flag,
      houseFlag: options.flag,
      releaseUserId: options.releaseUserId ? options.releaseUserId:this.data.userInfo.id
    })
    if (options.page =='wholeMine'){
      this.setData({
        wholeMine:true
      })
      wx.setNavigationBarTitle({
        title: '我的房源',
      })
    }
    this.getHouseByper()
  },
  // 根据用户ID查询相关用户发布的所有卖房房源信息
  getHouseByper: function(){
    var _this = this;
    if (_this.data.houseType == 3 || !this.data.isHalf) {
      wx.request({
        url: app.globalData.httpApiCar+ 'tGyHouseRental/search/releaseUserId',
        data: {
          pageSize: 10,
          pageNo: _this.data.currentPage,
          // houseType: _this.data.houseType,
          releaseUserId: _this.data.releaseUserId
        },
        success: function (res) {
          _this.setData({
            loadSuccess: true
          })
          wx.hideLoading()
          if (res.data.success) {
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
            var houses = res.data.data.list;
            for (var i = 0; i < houses.length; i++) {
              if (houses[i].picture) {
                houses[i].picture = houses[i].picture.split(",");
              }
              if (houses[i].housingCharacteristics) {
                houses[i].housingCharacteristics = houses[i].housingCharacteristics.split(",");
              }
              _this.data.houseList.push(houses[i])
            }
            _this.setData({
              houseList: _this.data.houseList
            })
            if(_this.data.houseList.length == res.data.data.total) {
              _this.setData({
                hasData: false
              })
            }
          }
        }
      })
    }
    else{
      wx.request({
        url: app.globalData.httpApiCar+ 'tGyHouseSale/getHouseSalesWithUserIdByPage',
        data: {
          pageNumber: 10,
          currentPage: _this.data.currentPage,
          houseType: _this.data.houseType,
          releaseUserId: _this.data.releaseUserId
        },
        success: function (res) {
          _this.setData({
            loadSuccess: true
          })
          wx.hideLoading()
          if (res.data.success) {
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
            if (res.data.data.houses){
            var houses = res.data.data.houses;
            for (var i = 0; i < houses.length; i++) {
              if (houses[i].picture) {
                houses[i].picture = houses[i].picture.split(",");
              }
              if (houses[i].housingCharacteristics) {
                houses[i].housingCharacteristics = houses[i].housingCharacteristics.split(",");
              }
              _this.data.houseList.push(houses[i])
            }
            _this.setData({
              houseList: _this.data.houseList
            })
            if (_this.data.houseList.length == res.data.data.page.totalNumber) {
              _this.setData({
                hasData: false
              })
            }
          }
          }
        }
      })
    }
  },
  // 进入详情页
  enterDetail: function (e) {
    var flag = e.currentTarget.dataset.flag;
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/houseDetail/houseDetail?flag=' + flag + '&name=' + item.title + '&id=' + item.id + '&communityName=' + item.communityName
    })
  },
  // 改变房源类型
  changeType: function(){
    this.setData({
      ['currentPage']: 1,
      houseList: [],
      hasData: true,
      isHalf: !this.data.isHalf
    })
    this.getHouseByper()
  },

  // 打电话
  toggleSelect: function (e) {
    var show = e.currentTarget.dataset.value == "true" ? true : false;
    this.setData({
      showSelect: show,
      //current_tel: e.currentTarget.dataset.tel
    })
  },
  callPhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.value
    })
    this.setData({
      showSelect: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.setData({
      ['currentPage']: 1,
      houseList: []
    })
    this.getHouseByper();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasData) {
      this.setData({
        ['currentPage']: this.data.currentPage + 1
      })
      this.getHouseByper();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})